package com.ricchieri.reports;

import java.util.Date;
import java.util.Map;

import com.ricchieri.common.BusinessSettings;
import com.ricchieri.model.ScheduleCharter;
import com.ricchieri.service.interf.IScheduleCharterService;
import com.ricchieri.util.FacesUtil;

public class ScheduleCharterStatusReport extends ReportTemplate {
	private static final long serialVersionUID = 3745017294788565719L;
	private ScheduleCharter scheduleCharter;
	
	@Override
	protected void addParameters(Map<String, Object> parameters) {
		if(scheduleCharter.getCharter()!= null) {
			parameters.put(BusinessSettings.REPORT_CHARTER_NUMBER, scheduleCharter.getCharter().getCharterNumber());
		}
		
		parameters.put(BusinessSettings.REPORT_SCHEDULE_DATE, scheduleCharter.getSchedule().getDate());
		parameters.put(BusinessSettings.REPORT_SCHEDULE_TIME, scheduleCharter.getSchedule().getTime());
		parameters.put(BusinessSettings.REPORT_TRIP_ORIENTATION, scheduleCharter.getTrip().getFormatTrip());
		
		if(scheduleCharter.getUser()!= null) {
			parameters.put(BusinessSettings.REPORT_DRIVER_NAME, scheduleCharter.getUser().getFullName());
		}
	}

	@Override
	public void generateReport(Map<String, Object> map,
			ReportFileFormat reportFileFormat) {
		 
		initReport(BusinessSettings.SCHEDULE_CHARTER_STATUS_REPORT, reportFileFormat);
		
		scheduleCharter = (ScheduleCharter) map.get(BusinessSettings.KEY_SCHEDULE_CHARTER);
		IScheduleCharterService scheduleCharterService  = (IScheduleCharterService) map.get(BusinessSettings.KEY_SERVICE);
		
		try {
			String result = doWork(scheduleCharter.getActiveReservations());
			
			if(!BusinessSettings.SUCCESS.equals(result)){
				FacesUtil.showInfoMessage("exportReport", "reportError");
			} else {
				scheduleCharter.setLastReportUpdate(new Date(System.currentTimeMillis()));
				scheduleCharterService.updateScheduleCharter(scheduleCharter);
				FacesUtil.showInfoMessage("exportReport", "reportSuccess");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			FacesUtil.showInfoMessage("exportReport", "reportError");
		}

	}
}
